package com.yst.gateway.monitor.common.filters;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * 解决get取参乱码问题，重写req.getParameter
 * @author sheungxin
 *
 */
public class GetFilter implements Filter{
	private String encoding;
	
	@Override
	public void init(FilterConfig filterConfig){
		encoding =filterConfig.getInitParameter("encoding");
	}
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		if(req.getMethod().equalsIgnoreCase("get")) {  
            req = new GetHttpServletRequestWrapper(req,encoding);  
        }
		chain.doFilter(req, response);
	}
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}
}
